package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLog;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLogConstant;
import com.cskaoyan.wordmemorize.converter.GoodsConverter;
import com.cskaoyan.wordmemorize.dao.entity.GoodsDO;
import com.cskaoyan.wordmemorize.dao.mapper.GoodsMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteGoodsOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.GoodsDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.GoodsCommand;
import com.cskaoyan.wordmemorize.request.GoodsPageRequest;
import com.cskaoyan.wordmemorize.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsConverter goodsConverter;

    public PageDTO<GoodsDTO> getGoodsPage(GoodsPageRequest request) {
        //转换
        if (request.getPageNum() == null) {
            LambdaQueryWrapper<GoodsDO> queryWrapper = new LambdaQueryWrapper<>();
            if (!request.getTypes().isEmpty()) {
                System.out.println("不是空");
                queryWrapper.in(GoodsDO::getType, request.getTypes());
            }
            List<GoodsDO> goodsDOS = goodsMapper.selectList(queryWrapper);
            return goodsConverter.goodsPage2PageDTO(goodsDOS, (long) goodsDOS.size());
        }
        Page<GoodsDO> page = new Page<>(request.getPageNum(), request.getCount());
        LambdaQueryWrapper<GoodsDO> queryWrapper1 = new LambdaQueryWrapper<>();
        if (request.getKeyword() != null) {
            queryWrapper1.like(GoodsDO::getName, request.getKeyword());
        }


        List<GoodsDO> goodsDOS = goodsMapper.selectList(page, queryWrapper1);

        return goodsConverter.goodsPage2PageDTO(goodsDOS, page.getTotal());


    }

    @Override
    @OperateLog(dateType = "商品", operateType = OperateLogConstant.OPERATE_ADD)
    public void addGoods(GoodsCommand command) {
        //转换
        System.out.println(command);
        GoodsDO Do = goodsConverter.command2GoodsDO(command);
        Do.setType(command.getType());
        System.out.println("转换后的商品" + Do);
         //判断商品是否存在更具名字
        LambdaQueryWrapper<GoodsDO> queryWrapper = new   LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsDO::getName, Do.getName());
        GoodsDO goodsDO = goodsMapper.selectOne(queryWrapper);
        if (goodsDO != null){
            //throw new BusinessException(ResultCodeEnum.GOODS_EXIST);
        }else {
            goodsMapper.insert(Do);
        }

    }

    @Override
    @OperateLog(dateType = "商品", operateType = OperateLogConstant.OPERATE_UPDATE)
    public void updateGoods(GoodsCommand command) {
        GoodsDO Do = goodsConverter.command2GoodsDO(command);
        Do.setType(command.getType());
        LambdaQueryWrapper<GoodsDO> queryWrapper = new   LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsDO::getId, Do.getId());
        goodsMapper.update(Do, queryWrapper);
    }

    @Override
    @OperateLog(dateType = "商品", operateType = OperateLogConstant.OPERATE_SINGLE_DELETE)
    public DeleteGoodsOperateDTO deleteGoods(Long goodsId) {
        LambdaQueryWrapper<GoodsDO> queryWrapper = new   LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsDO::getId, goodsId);
        GoodsDO goodsDO = goodsMapper.selectOne(queryWrapper);
        //删除
        goodsMapper.delete(queryWrapper);
        return goodsConverter.goodsDO2DeleteGoodsDTO(goodsDO);
    }

    @Override
    @OperateLog(dateType = "商品", operateType = OperateLogConstant.OPERATE_BATCH_DELETE)
    public List<DeleteGoodsOperateDTO> deleteGoodsSelected(BatchDeleteCommand command) {
        List<String> stringIds = command.getIds();
        //把ids变成long类型的
        List<Long> longIds = stringIds.stream()
                .map(Long::parseLong)
                .collect(Collectors.toList());



        //查询
        List<GoodsDO> goodsDOS = goodsMapper.selectByIds(longIds);


        int i = goodsMapper.deleteByIds(longIds);
         return goodsConverter.goodsDOs2DeleteGoodsDTOs(goodsDOS);
    }

    @Override
    @OperateLog(dateType = "商品", operateType = OperateLogConstant.OPERATE_UPDATE)
    public void updateGoodsStatus(Long goodsId, Integer status) {

        LambdaQueryWrapper<GoodsDO> queryWrapper = new   LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsDO::getId, goodsId);
        GoodsDO goodsDO = goodsMapper.selectOne(queryWrapper);
        goodsDO.setStatus(status);
        goodsMapper.update(goodsDO, queryWrapper);

    }
}
